package com.csbr.prod.Controller;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.csbr.prod.Dao.FileDao;
import com.csbr.prod.Dao.TotalImageDao;
import com.csbr.prod.Pojo.FileAttr;
import com.csbr.prod.Pojo.TotalImageEntity;
import com.csbr.prod.Utils.SendMessage;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@RestController
public class TotalImageController {


    @Autowired
    private TotalImageDao totalImageDao;

    @Autowired
    private FileDao fileDao;

    @RequestMapping("/indexImage")
    public SendMessage indexImage(){
        return SendMessage.Success(totalImageDao.findAll());
    }

    @RequestMapping("/HhhImage")
    public SendMessage HhhImage(){
        return SendMessage.Success(totalImageDao.findByType("type5"));
    }
    
    @RequestMapping("/OthersImage")
    public SendMessage OthersImage(){
        return SendMessage.Success(totalImageDao.findByType("type4"));
    }

    @RequestMapping("/SkyImage")
    public SendMessage SkyImage(){
        return SendMessage.Success(totalImageDao.findByType("type3"));
    }
   
    @RequestMapping("/OtherImage")
    public SendMessage OtherImage(){
        return SendMessage.Success(totalImageDao.findByType("type2"));
    }

    @RequestMapping("/NatureImage")
    public SendMessage NatureImage1(){
        return SendMessage.Success(totalImageDao.findByType("type1"));
    }

    @RequestMapping("/DeletePhotos/{PhotosId}")
    public void DeletePhotos(@PathVariable Integer PhotosId){
        totalImageDao.delete(PhotosId);
    }

    @RequestMapping("/UpdatePhotos")
    public SendMessage UpdatePhotos(@RequestBody TotalImageEntity photosjsons){
        System.out.println(photosjsons);
        totalImageDao.save(photosjsons);
        return SendMessage.Success(totalImageDao.findAll());
    }

    @RequestMapping("/SearchPhotos")
    public SendMessage SearchPhotos(@RequestBody TotalImageEntity photojsons){
        String type=photojsons.getType();
        String info=java.net.URLDecoder.decode(photojsons.getInfo());
        System.out.println(totalImageDao.findByTypeLikeOrInfoLike(type+'%',info+'%'));
        return SendMessage.Success(totalImageDao.findByTypeLikeOrInfoLike(type+'%',info+'%'));
    }

    @RequestMapping("/save")
    public void save(HttpServletRequest request,MultipartFile pic) throws Exception {
        System.out.println("start");
       //文件名
        FileAttr file=new FileAttr();
        String fileName = pic.getOriginalFilename();
        file.setFileName(fileName);
        //文件后缀
        String ex=FilenameUtils.getExtension(fileName);
        //时间戳
        String timeFileName = String.valueOf(new Date().getTime());
        //文件名对应的时间戳名
        String currentFilename=timeFileName+"."+ex;
        file.setCurrentFilename(currentFilename);
        String saveDir="G:\\Photograph\\static\\img";
        Files.copy(pic.getInputStream(), Paths.get(saveDir, currentFilename));
        TotalImageEntity photos=new TotalImageEntity();

        photos.setSrc("static/img/"+currentFilename);
        photos.setHref("static/img/"+currentFilename);
        photos.setInfo("");
        photos.setType("");
       
        totalImageDao.save(photos);
        // return "成功";
    }

}
